const http = require('http')
const fs = require('fs')
const path = require('path')
const url = require('url')

var reqUrl = 'http://nodejs.cn'
//讲url字符串转换为对象结构
var obj = url.parse(reqUrl)

var options = {
    hostname:'nodejs.cn',
    port:80,
    path:'/',
    method:'GET'
}

var request = http.request(options,(res)=>{
    var str = ''
    res.on("data",(chunk)=>{
        str += chunk
    })
    res.on("end",()=>{
        var filePath = path.join(__dirname,'/index.html')
        fs.writeFileSync(filePath,str)
    })
})
//http.request方法必须调用end方法
request.end()